package validate

import (
	"github.com/asaskevich/govalidator"
	"github.com/gin-gonic/gin"
	"niu-video/pkg/logger"
	"niu-video/pkg/response"
	"strings"
)

func Validate(c *gin.Context, obj interface{}) bool {

	// 1. 解析请求，支持 JSON 数据、表单请求和 URL Query
	if err := c.ShouldBind(obj); err != nil {
		logger.LogIf(err)
		response.FailWithMessage("请求解析错误，请确认请求格式是否正确", c)
		return false
	}
	// 2. 参数校验
	if _, err := govalidator.ValidateStruct(obj); err != nil {
		msg := strings.Split(err.Error(), ";")
		response.FailWithMessage(msg[len(msg)-1], c)
		return false
	}
	return true
}
